<script type="text/javascript" charset="utf-8">
	var matchInfos = new Array();

	function showPredictDialog(num) {
		resetBalls();
		match = matchInfos['n'+num];
		
		// Enable/Disable buttons
		$("#predictioneditor button").removeAttr("disabled");
		if (matchInfos['n'+(num-1)] == undefined)
			$("#predictioneditor button#prev").attr("disabled", "disabled");
		if (matchInfos['n'+(num+1)] == undefined)
			$("#predictioneditor button#next").attr("disabled", "disabled");
		
		
		$("#predictioneditor input#num").attr("value", num);
		
		$("#predictioneditor th.name_teamh").html(match["team_h_short"]);
		$("#predictioneditor th.name_teama").html(match["team_a_short"]);


		tipph = parseInt($("input#tipp_h_"+ match["id"]).attr("value"));
		tippa = parseInt($("input#tipp_a_"+ match["id"]).attr("value"));
		if (tipph > 0)
			ballClicked($("#predictioneditor #" + tipph + "ballh"));
		if (tippa > 0)
			ballClicked($("#predictioneditor #" + tippa + "balla"));
	}

	function setPredict(callb) {
		num = $("#predictioneditor input#num").attr("value");
		match = matchInfos['n'+num];

		mid = match["id"];
		tipph = $("#predictioneditor #teamh").html();
		tippa = $("#predictioneditor #teama").html();

		$("input#tipp_h_"+ mid).attr("value", tipph);
		$("input#tipp_a_"+ mid).attr("value", tippa);
		$("td#prediction"+ mid).html(tipph + " : " + tippa);

		if (callb === "next") {
			num++;
			showPredictDialog(num);
		}
		else if (callb === "prev") {
			num--;
			showPredictDialog(num);
		}

	}
	
	function resetBalls() {
		$("#predictioneditor div.ball").removeClass("active");
		$("#predictioneditor #teamh").html(0);
		$("#predictioneditor #teama").html(0);
	}

	function ballClicked(ball) {
		searchStr = "." +$(ball).attr('class').replace(" ",".").replace(" active","");
		value = parseInt($(ball).attr('id'));

		if (searchStr.indexOf("teama") == -1)
			team = "teamh";
		else
			team ="teama";
		inputField = $("#predictioneditor #"+team);

			
		
		// Wenn man einen Ball nochmal anklickt wird dieser wieder abgewählt
		if (''+value === inputField.html()) {
			value--;
		}				

		$(searchStr).each(function (i) {
			id = parseInt(this.id);
			// console.log(id, "<=", value);
			// active/inactive
			if (id <= value)
				$(this).addClass("active");	
			else
				$(this).removeClass("active");
		});

		// change input-box	
		inputField.html(value);
		return true;
	}

</script>
	

<? include('matches-matchgroup.php');?>

<?php if ($isOwner): ?>
	<?=form_open('matches/insert', array('id' => 'matchForm'));?>
<?php endif; ?>

<table id="matches" cellpadding="2" width="100%" cellspacing="2">

<thead><tr>
	<th id="matchgroup"> </th>
	<th ><?=lang('match');?></th>
	<th>Tipp</th>
	<th>Erg.</th>
	<th> </th>
</tr></thead>
	
<tfoot></tfoot>

<tbody>

<?php $summe=0; $i=0; foreach($matches as $match): ?>

<?php if ($i++ % 2 == 0) $class="odd"; else  $class="even"; ?>

<tr class="<?=$class?>">
	<td class="right"><? printf(lang('matchgroup_type_short'), $match->matchgroup);?></td>

<?php if ($match->isStarted): ?>

	<td class="match"><a href="<?=site_url('matches/match/'.$match->id)?>"><?=$match->team_h_three?> - <?=$match->team_a_three?></a></td>
	<td class="right"><?=$match->goals_h?> : <?=$match->goals_a?></td>
	<td class="right"><?=$match->tipp_h?> : <?=$match->tipp_a?></td>
	<td><? $summe += getPoints($match->goals_h, $match->goals_a, $match->tipp_h, $match->tipp_a); ?></td>
<?php elseif ($isOwner): ?>

	<td class="match">
		<?=$match->team_h_three?> - <?=$match->team_a_three?>
		<input name="id[]" type="hidden" class="hidden" value="<?=$match->id?>" />
		<input id="tipp_h_<?=$match->id?>" class="hidden" type="hidden" name="tipp_h[]" value="<?=$match->tipp_h?>" />
		<input id="tipp_a_<?=$match->id?>" class="hidden" type="hidden" name="tipp_a[]" value="<?=$match->tipp_a?>" />

		<script type="text/javascript">
			matchInfos["n<?=$i?>"] = <?=json_encode($match);?>;
		</script>
	</td>
	<td class="center" id="prediction<?=$match->id?>" onclick="showPredictDialog(<?=$i?>)">
	<?=$match->tipp_h?> : <?=$match->tipp_a?>
	</td>
	<td class="date" colspan="2"><?=formatDate($match->deadline)?></td>
<?php else: ?>
	<td class="center" colspan="2">? : ?</td>
	<td class="date"><?=formatDate($match->deadline)?></td>
<?php endif; ?>
</tr>
<?php endforeach; ?>

<tr class="pointssum">
	<td colspan="4"></td>
	<td class="right"><strong>&Sigma; = <?=$summe?></strong></td>
</tr>
	
</tbody></table>

<br/>
<?php if ($isOwner): ?>
	<input type="hidden" name="redirect" value="<?=$redirect?>"/>
	<a href="#predict" onclick="submit();" class="button"><?=lang('predict');?></a>
<?=form_close();?>
<?php endif; ?>


<div id="predictioneditor">
	<form><input type="hidden" value="" id="num" name="num" /></form>
	<table align="center" border="0" cellpadding="2" cellspacing="2">
	<thead><tr>
		<th class="name_teamh">Team H</th>
		<th class="name_teama">Team A</th>
	</tr></thead>
	<tfoot><tr>
		<th class="name_teamh">Team H</th>
		<th class="name_teama">Team A</th>
	</tr></tfoot>
	<tbody>
	
	<?php for($i=6;$i>0;$i--): ?>
	<tr>
		<td align="center"><div onclick="ballClicked(this)" id="<?=$i?>ballh" class="ball teamh"></div></td>
		<td align="center"><div onclick="ballClicked(this)" id="<?=$i?>balla" class="ball teama"></div></td>
	</tr>
	<?php endfor; ?>
	</tbody>
	
	<tr>
		<td class="goals" id="teamh">0</td>
		<td class="goals" id="teama">0</td>
	</tr>
	
	</table>
	<div class="btns">
		<button id="prev" onclick="setPredict('prev');">Zurück</button>
		<button onclick="setPredict();">Fertig</button>
		<button id="next" onclick="setPredict('next');">Weiter</button>
	</div>

</div>

